package com.example.eci_quartz.cloudcc.dao;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

public class baseDao {
	/*
	 * 登录获取Binding值
	 */
	public static String InsertXMInfo()  {
		// 获取配置文件内容
		ResourceBundle bundle = null;

		// 获取用户名
		String login_str;
		String result="";
		try {
			bundle = PropertyResourceBundle.getBundle("config");
			String userName = bundle.getString("com.cloudcc.username");
			String password = bundle.getString("com.cloudcc.password");
			//String userName = "";
			//String password ="";
			String data_login = "serviceName=clogin&userName="+userName+"&password="+password;
			login_str = operate(data_login);
			String bxdinfo ="["+login_str+"]";//手动转换成json格式，方便取值
				if(bxdinfo!="[]"){
				     JSONArray brand_list = JSONArray.fromObject(bxdinfo);
				     if(brand_list!=null){
						for(int j=0;j<brand_list.size();j++){
						   JSONObject brandInfo = (JSONObject)brand_list.get(j);
						   String Binding=brandInfo.get("binding")==null?"":brandInfo.get("binding")+"";// 获取Binding值
						   String Result=brandInfo.get("result")==null?"":brandInfo.get("result")+"";//获取返回值
						   if("true".equals(Result)){
							   result=Binding;
						   }
						}
				      }
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;

	}


	/**
	*  Web 服务应用实例
	* @param url_str CIp Web 服务系统的统一请求地址,现在用的是测试系统的请求地址。
	* @param data_str 业务数据字符串
	* @throws Exception
	*/
	public static String operate(String data_str){ //连接cloudcc系统(post)
		// 获取配置文件内容
		ResourceBundle bundle = PropertyResourceBundle.getBundle("config");
		String url_str = bundle.getString("com.cloudcc.strurl");//链接地址
        URL url=null;
        HttpsURLConnection con =null;
        OutputStreamWriter out =null;
        InputStream is =null;
        BufferedReader br =null;
		String sc = "";
		String st = "";
        try {
        	//设置访问协议
            System.setProperty("https.protocols","TLSv1.2");
            SSLContext ssf = SSLContext.getInstance("TLSv1.2");
            //初始化协议
			ssf.init(null, new TrustManager[] {
				  new MyX509TrustManager()
				  }, new java.security.SecureRandom());
			url = new URL(url_str);
			con = (HttpsURLConnection)url.openConnection();
			//设置协议
			con.setSSLSocketFactory(ssf.getSocketFactory());
			con.setHostnameVerifier(new TrustAnyHostnameVerifier());
			con.setRequestMethod("POST"); //post  方式请求
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setUseCaches(true);
			con.setConnectTimeout(500000);
			con.setReadTimeout(500000);
			out = new OutputStreamWriter(con.getOutputStream(), StandardCharsets.UTF_8);
			out.write(data_str);
			out.close();
			if (con.getResponseCode() == 200){
				is = con.getInputStream();
				br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
				while ((sc = br.readLine()) != null) {
					st = st + sc;
				}
			}else{
			}
			con.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if (is != null) {
					is.close();
					is = null;
				}
				if (br != null) {
					br.close();
					br = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return st;
	}
    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
        @Override
		public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }


}
